package com.kipling.kio.core;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import com.alibaba.fastjson.JSON;
import com.kipling.kio.core.intf.Packet;
import com.kipling.kio.server.ServerGroupContext;

/** 
 * 发送消息
 * @author KiplingLee kiplinglee@163.com
 * @version 创建时间：2017年5月11日 下午5:02:06
 * 
 */
public class Kio {
	
	public static < P extends Packet, R> void sendToUser(ServerGroupContext< P , R> serverGroupContext,String userid,P packet){
		SocketChannel socketChannel = serverGroupContext.getSocketChannel(userid);
		try {
			if(socketChannel!=null)
				socketChannel.write(ByteBuffer.wrap(JSON.toJSONString(packet).getBytes("utf-8")));
		} catch (UnsupportedEncodingException e) {
			
		} catch (IOException e) {
			
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
